﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using KinectTD.Agents;
using KinectTD.Input;
namespace KinectTD.Towers
{
    class PoisonTower : Tower
    {

        public PoisonTower(Vector2 towerPosition, Texture2D towerTexture, Texture2D radiusTexture, List<Enemy>enemyList, Texture2D bulletTexture)
            : base(towerPosition, towerTexture, radiusTexture, enemyList, bulletTexture)
        {
            rangeRadius = 250;
            cost = 200;
            TowerType = BULLET_TYPE.POISON;
            damage = 0.5f;
        }

        public override void Update(GameTime gameTime)
        {
            if (active)
            {
                for (int i = 0; i < activeBullets.Count; ++i)
                {
                    Bullet b = activeBullets[i];

                    //Update the survivors
                    b.Update(gameTime);

                    //Remove any bullets which are not in our radius anymore
                    Vector2 pDiff = Origin - b.Origin;
                    float distance = pDiff.Length();

                    if (!CheckBullet(b))
                    {
                        activeBullets.Remove(b);
                        continue;
                    }

                    if (!Engine.CheckCollision(Origin, b.Origin, rangeRadius, b.CollisionRadius))
                    {

                        activeBullets.Remove(b);
                        continue;

                    }



                }

                //If it is time to shoot - then shoot.
            
                    foreach (Enemy e in enemyList)
                    {
                        //If the enemy is in the RECTANGLE of the tower radius, then target it (adds for a more realistic, close enough, even though they can't be hit in this position)
                        if (radiusRangeRect.Contains((int)e.Position.X, (int)e.Position.Y))
                        {


                            Vector2 v = new Vector2(e.Position.X - e.Size.X / 2, e.Position.Y - e.Size.Y / 2);
                            Bullet b = new Bullet(Vector2.Zero, 0, v, ammoTexture, damage, enemyList);
                            b.BTYPE = TowerType;
                            b.Size = e.Size * new Vector2(2);
                           

                            activeBullets.Add(b);
                            break;
                        }

                    
                    timer = 0;
                }
            }
            if (drawRect.Contains(Kinect.CursorPosition) || drawRect.Contains(MouseHelper.MousePosition))
                isHovered = true;
            else
                isHovered = false;
        }

        private bool CheckBullet(Bullet bu)
        {
                for (int j = 0; j < enemyList.Count; ++j)
                {
                    Enemy e = enemyList[j];
                    if (bu.Position == e.Position)
                    {
                        return true;
                    }
                }

            return false;
        }
    }
}
